package com.example.administrator.hqq.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.utils.NotificationUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;

import okhttp3.Call;

public class DownloadAPKService extends Service {
    private static final String TAG = "DownloadAPKService";
    private Thread thread;
    private String url;
    private static final int MIN_DELAY_TIME = 1000;
    private static long lastTime=0;
    public DownloadAPKService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils.get().url(url)
                        .build().execute(new FileCallBack(Environment.getExternalStorageDirectory()
                        .getAbsolutePath(), "HQQ.apk") {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "onError: " + e.getMessage());
                    }

                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void inProgress(float progress, long total, int id) {
                        super.inProgress(progress, total, id);
                        Log.e(TAG, "inProgress: " + progress + "total    :" + total + "id :" + id);
                        long curClickTime = System.currentTimeMillis();
                        if (lastTime==0){
                            lastTime = curClickTime;
                        }
                        if ((curClickTime - lastTime) >= MIN_DELAY_TIME) {
                            NotificationUtils.showNotification("更新话圈圈", "正在下载" + (int) (progress * 100) + "%", 12, "12", (int) (progress * 100), 100, DownloadAPKService.this);
                            lastTime = curClickTime;
                        }
                        else{
                        }
                    }

                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onResponse(File response, int id) {
                        Log.e(TAG, "onResponse: " + "onResponse");
                        NotificationUtils.cancleNotification(12, DownloadAPKService.this);
                        showSelectAPK(DownloadAPKService.this);
                    }
                });
            }
        });
    }
    public int onStartCommand(final Intent intent, int flags, int startId) {
        url=intent.getStringExtra(LoongsConfig.url);
        thread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onDestroy() {
        super.onDestroy();
        NotificationUtils.cancleNotification(12,DownloadAPKService.this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /***
     * 调起安装APP窗口  安装APP
     * @param context
     */
    private static void showSelectAPK(Context context){
        File fileLocation = new File(Environment.getExternalStorageDirectory(), "HQQ.apk");//APK名称
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(fileLocation), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }
}
